]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
environment: Fallback to Soup 2.4
authorFlorian Müllner <fmuellner@gnome.org>
Wed, 4 Aug 2021 17:37:15 +0000 (19:37 +0200)
committerFlorian Müllner <fmuellner@gnome.org>
Tue, 24 Aug 2021 23:54:07 +0000 (01:54 +0200)
We cannot assume yet that Soup3 is available everywhere. Until that
is the case, allow falling back to Soup 2.4 by imitating the bits
of the Soup3 API we use with monkey-patching.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1940>

js/ui/environment.js

index d3ea54387ed8f8d1f43864ab933834f7b8cb331d..bd372ce26a5f3e2a2346b42b1b050a86c8c64312 100644 (file)
@@ -11,6 +11,14 @@ imports.gi.versions.Soup = '3.0';
 imports.gi.versions.TelepathyGLib = '0.12';
 imports.gi.versions.TelepathyLogger = '0.2';
 
+try {
+    const Soup_ = imports.gi.Soup;
+} catch (e) {
+    imports.gi.versions.Soup = '2.4';
+    const { Soup } = imports.gi;
+    _injectSoup3Compat(Soup);
+}
+
 const { Clutter, Gio, GLib, GObject, Meta, Polkit, Shell, St } = imports.gi;
 const Gettext = imports.gettext;
 const System = imports.system;
@@ -69,6 +77,44 @@ function _patchLayoutClass(layoutClass, styleProps) {
     }
 }
 
+/**
+ * Mimick the Soup 3 APIs we use when falling back to Soup 2.4
+ *
+ * @param {object} Soup 2.4 namespace
+ * @returns {void}
+ */
+function _injectSoup3Compat(Soup) {
+    const ByteArray = imports.byteArray;
+
+    Soup.StatusCode = Soup.KnownStatusCode;
+
+    Soup.Message.new_from_encoded_form =
+        function (method, uri, form) {
+            const soupUri = new Soup.URI(uri);
+            soupUri.set_query(form);
+            return Soup.Message.new_from_uri(method, soupUri);
+        };
+    Soup.Message.prototype.set_request_body_from_bytes =
+        function (contentType, bytes) {
+            this.set_request(
+                contentType,
+                Soup.MemoryUse.COPY,
+                ByteArray.toString(bytes.get_data()));
+        };
+
+    Soup.Session.prototype.send_and_read_async =
+        function (message, prio, cancellable, callback) {
+            this.queue_message(message, () => callback(this, message));
+        };
+    Soup.Session.prototype.send_and_read_finish =
+        function (message) {
+            if (message.status_code !== Soup.KnownStatusCode.OK)
+                return null;
+
+            return message.response_body.flatten().get_as_bytes();
+        };
+}
+
 function _makeEaseCallback(params, cleanup) {
     let onComplete = params.onComplete;
     delete params.onComplete;